/*
 * author      : 吴中勤
 * date        : 2018/8/22
 * description : 车型转换 sap -> moby
 */

package com.deepexi.adapter.rest.sap.parser;

import com.deepexi.adapter.common.StringUtil;
import com.deepexi.adapter.facade.sap.request.SapPushModelBody;
import com.deepexi.adapter.sdk.moby.request.VehicleModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

public class SapPushModelParser extends BaseParser<VehicleModel, SapPushModelBody> {

    @Override
    protected void convertEachSapItem(SapPushModelBody sapItem) {

        if (StringUtil.isNullorEmpty(sapItem.getModelLine())) {
            logger.warn("车型同步,缺少 车系, {}", sapItem.uglyString());
            return;
        }
        if (StringUtil.isNullorEmpty(sapItem.getMcodeSd())) {
            logger.warn("车型同步,缺少 车型代码, {}", sapItem.uglyString());
            return;
        }
        if (StringUtil.isNullorEmpty(sapItem.getModelText())) {
            logger.warn("车型同步,缺少 车型文本, {}", sapItem.uglyString());
            return;
        }

        VehicleModel mobyItem = new VehicleModel();
        mobyItem.setVehicleModelLineCode(sapItem.getModelLine());
        mobyItem.setCode(sapItem.getMcodeSd());
        mobyItem.setName(sapItem.getModelText());
        mobyRequestBodyList.add(mobyItem);
    }

    @Override
    protected List<SapPushModelBody> parseSapListFromRecordsStr(String recordsStr) {
        Type collectionType = new TypeToken<List<SapPushModelBody>>() {
        }.getType();
        return new Gson().fromJson(recordsStr, collectionType);
    }
}
